/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.ClientPhone;
import br.com.jr2tecnologia.model.entity.KeyResponse;
import br.com.jr2tecnologia.model.entity.Login;
import br.com.jr2tecnologia.model.entity.Modem;
import br.com.jr2tecnologia.model.entity.Page;
import br.com.jr2tecnologia.model.entity.Role;
import br.com.jr2tecnologia.model.persistence.DAOException;
import br.com.jr2tecnologia.model.persistence.DAOFactory;
import br.com.jr2tecnologia.model.repository.ClientPhoneRepository;
import br.com.jr2tecnologia.model.repository.LoginRepository;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author ronildo
 */

@Stateless
public class LoginPersistence implements LoginPersistenceLocal{

    private static final Logger logger = LoggerFactory.getLogger();

    @PersistenceContext
    private EntityManager em;
    private LoginRepository loginRepository;
    private ClientPhoneRepository clientPhoneRepository;

    @PostConstruct
    public void initialize(){
        loginRepository = DAOFactory.getLoginRepository(em);
        clientPhoneRepository = DAOFactory.getClientPhoneRepository(em);
    }

    public void persist(Login login){
        loginRepository.persist(login);
    }

    public void update(Login login){
        loginRepository.update(login);
    }

    public void remove(Login login){
        loginRepository.remove(login, login.getPkLogin());
    }

    public Login findById(Login login){
        Login entity = null;
        try{
            entity = loginRepository.findById(login.getPkLogin());
        }catch(DAOException e){
            logger.log(Level.SEVERE, "canot find login for this name: " + login.getPkLogin(), e);
        }
        return entity;
    }

    public Login findLoginByName(String login) throws PersistenceUnitException{
        Login entity = null;
        try{
            entity = loginRepository.findByName(login);
        }catch(DAOException ex){
            logger.severe("cannot find login for this name: " + login);
            throw new PersistenceUnitException(ex);
        }
        return entity;
    }

    public Login findLoginByEmail(String email){
        Login entity = null;
        try{
            entity = loginRepository.findByEmail(email);
        }catch(DAOException e){
            logger.log(Level.SEVERE, "canot find login for this email: " + email, e);
        }
        return entity;
    }

    public List<Login> list(Login login){
        try{
            return loginRepository.list(login);
        }catch(DAOException e){
            logger.log(Level.SEVERE, "cannot list login", e);
            throw new DAOException(e);
        }
    }

    public List<Modem> listModensOfLogin(Login entity){
        Login login = loginRepository.findById(entity.getPkLogin());
        return new LinkedList<Modem>(login.getModemList());
    }

    public List<Login> listAll(){
        try{
            return loginRepository.listAll();
        }catch(DAOException e){
            logger.log(Level.SEVERE, "canot list login", e);
            throw new DAOException(e);
        }
    }

    public boolean loginHasPermissionForResource(Login entity, String resource) throws PersistenceUnitException{
        try {
            entity = loginRepository.findById(entity.getPkLogin());
            Role role = entity.getRole();
            List<Page> pages = role.getPageList();
                for (Page page : pages)
                    if (page.getLink().equals(resource)) 
                        return true;

            return false;
        } catch (DAOException ex) {
            logger.severe("cannot find entity "+entity.getName()+" in database");
            throw new PersistenceUnitException(ex);
        }
    }

    public List<ClientPhone> listAllClientPhone(Login entity){
        try{
            ClientPhone clientPhone = new ClientPhone();
            clientPhone.setLogin(entity);
            return clientPhoneRepository.list(clientPhone);
        }catch(DAOException e){
            logger.info("cannot list client phone from this client: " + entity.getName());
            return null;
        }
    }

    public List<KeyResponse> listAllKeyResponse(Login entity){
        try{
            List<KeyResponse> list = new LinkedList<KeyResponse>();
            Login login = loginRepository.findById(entity.getPkLogin());
            list.addAll(login.getKeyResponseList());
            return list;
        }catch(DAOException e){
            logger.info("cannot list KeyResponse from this login: " + entity.getName());
            return null;
        }
    }

    public Login verifyLogin(String email){
        Login entity = loginRepository.findByEmail(email);
        if(entity != null)
            entity.createRulesView();
        return entity;
    }
}
